<template>
  <div style="position: relative">
    <el-form
      :inline="true"
      size="mini"
      :model="formInline"
      label-position="right"
    >
      <el-form-item label="项目名称">
        <el-input
          class="filter-item"
          v-model="formInline.projectName"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="项目状态">
        <el-select
          v-model="formInline.verifyStatus"
          clearable
          placeholder="请选择"
        >
          <el-option label="全部" value=""></el-option>
          <!-- <el-option label="待立项" value="1"></el-option>
          <el-option label="立项中" value="2"></el-option>
          <el-option label="立项通过/执行中" value="3"></el-option>
          <el-option label="立项驳回" value="4"></el-option>
          <el-option label="任务完成/待归档" value="5"></el-option> -->
          <el-option label="归档中" value="6"></el-option>
          <el-option label="归档成功/待复盘" value="7"></el-option>
          <el-option label="归档驳回" value="8"></el-option>
          <el-option label="复盘中" value="9"></el-option>
          <el-option label="已完成" value="10"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="times"
          type="daterange"
          align="right"
          unlink-panels
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="selecttime"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="企业电话">
        <el-input
          class="filter-item"
          v-model="formInline.telephone"
          maxlength="11"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">搜索</el-button>
        <el-button
          type="warning"
          class="el-button--reset"
          icon="el-icon-refresh-right"
          @click="resetFromdata"
          size="mini"
        ></el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table
          :data="tableData"
          size="mini"
          style="width: 100%"
          header-cell-class-name="table-header-gray"
          v-loading="isShowloading"
        >
          <el-table-column
            type="index"
            align="center"
            width="55"
            label="序号"
          ></el-table-column>
          <el-table-column
            prop="projectName"
            align="center"
            label="项目名称"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="customerName"
            align="center"
            label="企业名称"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="contacts"
            align="center"
            label="企业联系人"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="telephone"
            align="center"
            label="企业电话"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="verifyStatus"
            align="center"
            label="项目状态"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{scope.row.verifyStatus | verifyStatusFilter}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="projectStatus"
            align="center"
            label="线索录入人"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{scope.row.tyName}},{{scope.row.tyDeptName}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="projectStatus"
            align="center"
            label="商机跟进人"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{scope.row.tcName}},{{scope.row.tcDeptName}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createUserName"
            align="center"
            label="项目负责人"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{scope.row.createUserName}},{{scope.row.deptName}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            align="center"
            label="项目创建时间"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="scope">
              <el-button type="text" @click="handleApprover(scope.row)" v-if="scope.row.isProjectTaskState === 2">审批</el-button>
              <el-button type="text" @click="handleInfo(scope.row)">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"
          ></el-pagination>
        </el-row>
      </el-col>
    </el-row>
    <el-dialog
      title="审批"
      :visible.sync="approverDialog"
      append-to-body
      width="800px"
      class="group-dialog"
    >
      <div class="approver-inquiry">
         <el-form :model="detailObj" size="mini" label-position="right">
        <el-row :gutter="20">
          <el-col :span="24" class="clue-title" style="display: block">
            项目信息
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称：">{{
              approverProject.projectName
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业名称：">{{
              approverProject.customerName
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业联系人：">
              <span>{{ approverProject.contacts }}</span>
              <span style="margin-left: 10px">{{
                approverProject.telephone
              }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目状态：">{{
              approverProject.projectStatus
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="线索录入人：">{{
              approverProject.projectStatus
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商机跟进人：">{{
              approverProject.projectStatus
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目负责人：">{{
              approverProject.createUser
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目创建时间:">{{
              approverProject.createTime
            }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="approverProject.projectInitiationTaskServiceOne" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray">
        <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
        <el-table-column prop="projectName" align="center" label="项目名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="task" align="center" label="文件/任务名称"></el-table-column>
        <el-table-column prop="accessoryList" align="center" label="附件">
          <template slot-scope="{row}">
            <span>{{row.accessoryList&&JSON.parse(row.accessoryList)&&JSON.parse(row.accessoryList).length>0?JSON.parse(row.accessoryList).map(item=>item.name).toString():''}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="UserName" align="center" label="创建人"></el-table-column>
        <el-table-column prop="createTime" align="center" label="创建时间"></el-table-column>
      </el-table>
        <el-form
          :model="ruleForm"
          ref="ruleForm"
          label-width="80px"
          size="mini"
          label-position="right"
        >
          <el-form-item label="是否通过" prop="state">
            <el-radio-group v-model="ruleForm.state">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="原因" prop="approvalOpinion">
            <el-input
              class="input-item"
              v-model="ruleForm.approvalOpinion"
              placeholder="请输入"
              maxlength="20"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="approverDialog = false;resetForm()" size="small">取 消</el-button>
        <el-button type="primary" @click="approverDialog = false;submit()" size="small">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog
      title=""
      :visible.sync="detailDialog"
      width="800px"
      destroy-on-close
    >
      <el-form :model="detailObj" size="mini" label-position="right">
        <el-row :gutter="20">
          <el-col :span="24" class="clue-title" style="display: block">
            项目信息
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称：">{{
              detailObj.projectName
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业名称：">{{
              detailObj.customerName
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业联系人：">
              <span>{{ detailObj.contacts }}</span>
              <span style="margin-left: 10px">{{
                detailObj.telephone
              }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目状态：">{{
              detailObj.projectStatus
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="线索录入人：">{{
              detailObj.projectStatus
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商机跟进人：">{{
              detailObj.projectStatus
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目负责人：">{{
              detailObj.createUser
            }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目创建时间:">{{
              detailObj.createTime
            }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="detailObj.tableDate" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray">
        <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
        <el-table-column prop="projectName" align="center" label="项目名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="task" align="center" label="文件/任务名称"></el-table-column>
        <el-table-column prop="accessoryList" align="center" label="附件">
          <template slot-scope="{row}">
            <span>{{row.accessoryList&&JSON.parse(row.accessoryList)&&JSON.parse(row.accessoryList).length>0?JSON.parse(row.accessoryList).map(item=>item.name).toString():''}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="UserName" align="center" label="创建人"></el-table-column>
        <el-table-column prop="createTime" align="center" label="创建时间"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { Administration } from '@/api/projectAdministration.js';
export default {
  name: 'ProjectArchiveCc',
  filters: {
    verifyStatusFilter (val) {
      const data = {
        1: '待立项',
        2: '立项中',
        3: '立项通过/执行中',
        4: '立项驳回',
        5: '任务完成/待归档',
        6: '归档中',
        7: '归档成功/待复盘',
        8: '归档驳回',
        9: '复盘中',
        10: '已完成'
      };
      return data[val];
    }
  },
  data () {
    return {
      approverDialog: false,
      detailDialog: false,
      detailObj: {},
      formInline: {
        projectName: '',
        telephone: '',
        startCreateTime: '',
        endCreateTime: '',
        verifyStatus: ''
      },
      tableData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false,
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }
        ]
      },
      ruleForm: {
        state: 1, // 审批是否通过
        approvalOpinion: ''// 审批意见/原因
      },
      times: '',
      approverProject: {} // 待审批项目
    };
  },
  computed: {
    ...mapGetters([
      'getUserInfo'
    ])
  },
  created () {
  },
  activated () {
    this.loadAttendanceWidgetData(); // 数据加载的方法
  },
  mounted () {
    this.loadAttendanceWidgetData();
  },
  methods: {
    // 处理审批
    handleApprover (row) {
      this.approverDialog = true;
      Administration.getArchiveInfo(this, {id: row.id})
        .then((res) => {
          this.approverProject = {
            ...row,
            ...res.data
          }
        })
    },
    resetForm () {
      this.ruleForm = {}
      this.approverProject = {}
    },
    submit () {
      let params = {
        ...this.ruleForm,
        projectId: this.approverProject.id
      }
      // 随便传一个任务id
      if (this.approverProject.projectInitiationTaskServiceOne?.length) {
        params.id = this.approverProject.projectInitiationTaskServiceOne[0].id;
      }
      Administration.archiveApproval(this, params)
        .then((res) => {
          console.log(res)
          this.$message({
            message: '审批成功',
            type: 'success'
          });
          this.loadAttendanceWidgetData();
        })
    },
    // 选择创建时间
    selecttime (times) {
      if (times) {
        this.formInline.startCreateTime = times[0];
        this.formInline.endCreateTime = times[1];
      } else {
        this.formInline.startCreateTime = '';
        this.formInline.endCreateTime = '';
      }
    },
    /**
     * 项目管理列表
     */
    loadAttendanceWidgetData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo,
        isState: '3'
      };
      Administration.getArchiveList(this, params)
        .then((res) => {
          this.tableData = res?.data?.records;
          this.total = res?.data?.total;
        })
    },
    // 搜索
    onSubmit () {
      this.pageNo = 1;
      this.loadAttendanceWidgetData();
    },
    // 重置
    resetFromdata () {
      this.formInline = {
        telephone: '',
        startCreateTime: '',
        endCreateTime: '',
        projectName: '',
        verifyStatus: ''
      };
      this.times = '';
      this.pageNo = 1;
      this.page_size = 10;
      this.loadAttendanceWidgetData();
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val;
      this.loadAttendanceWidgetData();
    },
    handleSizeChange (val) {
      this.page_size = val;
      this.loadAttendanceWidgetData();
    },
    // 查看
    handleInfo (row) {
      this.$router.push({
        path: '/ProjectsDetail',
        query: {
          projectId: row.id,
          verifyStatus: row.verifyStatus,
          businessType: row.businessType
        }
      });
    }
    // // 查看
    // handleInfo (row) {
    //   this.detailDialog = true
    //   Administration.getArchiveInfo(this, {id: row.id})
    //     .then((res) => {
    //       this.detailObj = {
    //         ...row,
    //         tableDate: res.data
    //       }
    //     })
    // }
  }
};
</script>
<style scoped lang="scss">
.clue-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 20px;
  margin-bottom: 10px;
}
</style>
